package pl.dsp.dsp1.ui;

import java.util.ArrayList;

import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import pl.dsp.dsp1.processing.Signal;

public class SignalListModel extends ArrayList<Signal> implements ListModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5447498182551944285L;

	private ArrayList<ListDataListener> listeners = new ArrayList<ListDataListener>();

	public void notifyListeners() {
		final SignalListModel that = this;
		for (final ListDataListener listener : listeners) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					listener.contentsChanged(new ListDataEvent(that,
							ListDataEvent.CONTENTS_CHANGED, 0, that.size() - 1));
				}
			});
		}
	}

	@Override
	public boolean add(Signal e) {
		notifyListeners();
		return super.add(e);
	}

	@Override
	public Signal remove(int index) {
		notifyListeners();
		return super.remove(index);
	}

	@Override
	public void addListDataListener(ListDataListener arg0) {
		listeners.add(arg0);
	}

	@Override
	public Object getElementAt(int arg0) {
		return this.get(arg0).getName();
	}

	@Override
	public int getSize() {
		return this.size();
	}

	@Override
	public void removeListDataListener(ListDataListener arg0) {
		listeners.remove(arg0);
	}

}
